﻿using MediatR;
using RedDragon.Common.Domain.Manufacture;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.WorkCenter
{
    public class Get2WorkCenterHandler : IRequestHandler<Get2WorkCenterCommand, Result>
    {
        /// <summary>
        /// 工作表仓储
        /// </summary>
        public readonly IBaseRepository<WorkCenters> workCenterRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GetWorkCenterHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="workCenterRepository"></param>
        /// <param name="logger"></param>
        public Get2WorkCenterHandler(IBaseRepository<WorkCenters> workCenterRepository, ILogger<GetWorkCenterHandler> logger)
        {
            this.workCenterRepository = workCenterRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 根据工作编码查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(Get2WorkCenterCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                //获取工作中心信息
                var list = await(await workCenterRepository.GetAll()).Where(d => d.WorkCenterCode == request.WorkCenterCode).FirstAsync();
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "根据工作编码获取工作信息成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据工作编码获取工作信息异常" + ex.Message;
                logger.LogError("根据工作编码获取工作信息异常" + ex.Message);
            }
            return result;
        }
    }
}
